<?php
session_start();

require_once 'view/LoginView.php';
require_once 'view/LogView.php';
require_once 'model/LoginHandler.php';
require_once 'control/LoginController.php';
require_once 'control/MasterController.php';
require_once 'control/RegistrerController.php';
require_once 'model/UserHandler.php';
require_once 'model/ProductHandler.php';
require_once 'model/LogHandler.php';
require_once 'model/ShopBasket.php';

	
class TestAll {

	public static function runTestDB($DBConnection) {
		
		if($DBConnection->Connect()== FALSE) {
			LogHandler::logError("EJ UPPKOPPLAD till DB i min TestAll-klass!");
			return;
		}
		else {
			
		}
		
	}
	
	public static function SelectAll($DBConnection) {
		
		if($DBConnection->Connect()== TRUE) {
			$sql = "SELECT * FROM user ORDER BY pk ASC";
			$stmt = $DBConnection->Prepare($sql);
			$stmt -> execute();
			
				$result = $stmt->get_result();
				while ($row = $result->fetch_array(MYSQLI_NUM))
				{
					foreach ($row as $r)
					{
					print "$r ";
					}
					print "</br>";
				}
				
				$stmt -> close();
			}
			
		}
	
	
	public static function SelectProducts ($DBConnection) {

		$ret = array();	
			
		$sql = "SELECT product_pk, m_category, m_productname, m_price FROM product";
		
		$stmt = $DBConnection->Prepare($sql);
		
		$stmt -> execute();
		$result = $stmt->get_result();
		
				while ($row = $result->fetch_array(MYSQLI_NUM))
				{
					foreach ($row as $r)
					{
					print "$r ";
					}
					print "</br>";
				}
				
				$stmt -> close();
		}
		
	
	public static function SelectAllBuyers ($DBConnection) {

		$ret = array();	
			
		$sql = "SELECT shopuser_pk, product_pk FROM shopbasket WHERE status = 1 ORDER BY product_pk ASC";
		
		$stmt = $DBConnection->Prepare($sql);
		
		$stmt -> execute();
						$result = $stmt->get_result();
				while ($row = $result->fetch_array(MYSQLI_NUM))
				{
					foreach ($row as $r)
					{
					print "$r ";
					}
					print "</br>";
				}
				
				$stmt -> close();
		}
	
		
		
	public function get($stmt){
		$ret = array();
		
		$stmt->bind_result($shopbasket_pk, $shopuser_pk, $m_shopprice, $product_pk, $status);
		
		while ($stmt->fetch()) {
			$shopbasket = new ShopBasket();
			$shopbasket->shopbasket_pk = $shopbasket_pk;
			$shopbasket->shopuser_pk =$shopuser_pk;
			$shopbasket->m_shopprice=$m_shopprice;
			$shopbasket->product_pk=$product_pk;
			$shopbasket->status=$status;

        	$ret[] = $shopbasket;
    	}
		
		return $ret;
	}

	public static function TestAllFinal($DBConnection) {
		$UserHandler = new UserHandler($DBConnection);
		$loginHandler = new LoginHandler($DBConnection);
		$RegisterController = new RegisterController($UserHandler);
		$productHandler = new ProductHandler($DBConnection);
		$shopBasketHandler = new ShopBasketHandler($DBConnection);
		$shopbasket = new ShopBasket($DBConnection);
		
		

		
//nya instanser
// Produkt
$productHandler = new ProductHandler($DBConnection);

//LogIn
$loginHandler = new LoginHandler($DBConnection);
$UserHandler = new UserHandler($DBConnection);
$loginView = new LoginView();
$LoginController = new LoginController($loginHandler, $UserHandler, $productHandler, $shopBasketHandler);
$RegisterController = new RegisterController($UserHandler);
$MasterController = new MasterController($LoginController, $RegisterController);
		
		

		$string = "";
		//------------TEST-------------- Om test lyckas


		if ($loginHandler -> Test() == TRUE) {
			$string .= "<p id='textStringRight'>Lyckat Test</p>";
		} else {
			$string .= "<p id='textStringWrong'>Misslyckat Test</p>";
		}
		
		$string .= $MasterController -> DoControll();

		//Kör testet
		$loginHandler -> Test();
		return $string;
	}
	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link rel="stylesheet" type="text/css" href="css/basic.css" title="default" />
		<title>LoginController</title>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	</head>
	<body>
		<div id="container">
			<?php 
				$DBConnection = new DBConnection();
				$DBConnection->Connect();
				echo TestAll::TestAllFinal($DBConnection);
				echo TestAll::runTestDB($DBConnection);
				echo "<p><b>Vem har köpt varor...</b></p>";
				echo "<hr>";
				echo "<p><b>NamnID: ProduktID:</b></p>";
				echo TestAll::SelectAllBuyers($DBConnection);
				echo "<hr>";
				echo "<p><b>Alla Reggade personer</b></p>";
				echo "<hr>";
				echo "<p><b>NamnID: Namn: E-post: Lösen:</b></p>";
				echo TestAll::SelectAll($DBConnection);
				echo "<hr>";
				echo "<p><b>Alla Produkter</b></p>";
				echo "<hr>";
				echo "<p><b>ProduktID: Kategori: Produkt: Pris: </b></p>";
				echo TestAll::SelectProducts($DBConnection);
				echo "<hr>";
				$DBConnection->Close();
			?>
		</div>
	</body>
</html>